package com.nazca.tvremote.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.EditText;

import com.nazca.tvremote.R;
import com.nazca.tvremote.service.Controller;
import com.nazca.tvremote.service.EventService;
import com.nazca.tvremote.util.RLog;
import com.nazca.tvremote.util.Settings;
import com.nazca.tvremote.view.TitleBar;

public class ConnectivityActivity extends Activity implements OnClickListener {
	
	private static final String TAG = "ConnectivityActivity";
	
	private TitleBar mTitleBar;
	
	private EditText mDstAddressEdit;
	private EditText mDstPortEdit;
	
	private Settings mSettings;
	
	private ControllerResults mControllerCallback = new ControllerResults();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Intent intent = getIntent();
        boolean isExit = intent.getBooleanExtra("EXIT", false);
        if(isExit){
        	finish();
        }
		
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
		setContentView(R.layout.connectivity);
		
		mTitleBar = (TitleBar) findViewById(R.id.title_bar);
		mTitleBar.getBackButton().setOnClickListener(new View.OnClickListener(){
			public void onClick(View v) {
				finish();
			}
		});
//		mTitleBar.getPowerButton().setOnClickListener(new View.OnClickListener(){
//			public void onClick(View v) {
//				finish();
//			}
//		});
		
		mDstAddressEdit = (EditText) findViewById(R.id.edit_ip);
		mDstPortEdit = (EditText) findViewById(R.id.edit_port);
		findViewById(R.id.btn_submit).setOnClickListener(this);
		
		mSettings = Settings.getSettings(this);
		
		mDstAddressEdit.setText(mSettings.mDstAddress);
		mDstPortEdit.setText(String.valueOf(mSettings.mDstPort));
		
//		startService(new Intent(this, EventService.class));
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		Controller.getInstance(getApplication()).addResultCallback(mControllerCallback);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		Controller.getInstance(getApplication()).removeResultCallback(mControllerCallback);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		Controller.getInstance(getApplication()).disConnectToServer();
	}
	
	private class ControllerResults implements Controller.Result{

		@Override
		public void connectToServer(boolean successful) {
			RLog.d(TAG, "connectToServer()");
			RLog.d(TAG, "successful:"+successful);
			if(successful){
				MainActivity.actionMain(ConnectivityActivity.this);
//				finish();
			}
		}

		@Override
		public void disConnectToServer(boolean successful) {
			RLog.d(TAG, "disConnectToServer()");
			RLog.d(TAG, "successful:"+successful);
		}
	}
	
	private void onSubmit(){
		mSettings.mDstAddress = mDstAddressEdit.getText().toString().trim();
		mSettings.mDstPort = Integer.parseInt(mDstPortEdit.getText().toString());
		mSettings.commit();
		
		Controller.getInstance(getApplication()).connectToServer(mSettings.mDstAddress, mSettings.mDstPort);
	}

	@Override
	public void onClick(View v) {
		onSubmit();
	}
	
}
